package de.herberlin.pss.action;

import java.io.File;

import de.herberlin.pss.DatabaseManager;
import de.herberlin.pss.ServiceLocator;
import de.herberlin.pss.model.Song;
import de.herberlin.pss.ui.SongTableModel;

public class DeleteAction extends AbstractProgressDialogAction{

	private int[] selectedRows = null;

	public DeleteAction(int[] selectedRows) {
		this.selectedRows = selectedRows;
	}

	@Override
	public void doPerform()  {
		DatabaseManager dbManager = ServiceLocator.getDatabaseManager();
		for (int i : selectedRows) {
			try {
				Song song = SongTableModel.getInstance().getSongAt(i);
				if (song.get(Song.Key.cFileName) != null) {
					File file = new File(ServiceLocator.getSqliteDir(), song
							.get(Song.Key.cFileName));
					if (file.exists()) {
						file.delete();
					}
				}
				dbManager.deleteSong(song);
				logger.info("Deleted: " + song);
			} catch (Exception ex) {
				logger.error(ex, ex);
			}
		}
		SongTableModel.getInstance().updateContent();
	}

	@Override
	protected void doRollback() throws Exception {
		// TODO: implemente me
	}

}
